//
//  LoginPageLevel2ViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-3-15.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "LoginPageLevel2ViewController.h"
#import "AppsEngine.h"
#import "SVProgressHUD.h"
#import "AppsLocationManager.h"
#import "UserSession.h"
#import "GFVOParser.h"
#import "GFVOs.h"
#import "AppDelegate.h"

@interface LoginPageLevel2ViewController ()<UITextFieldDelegate> {
    
}
@end

@implementation LoginPageLevel2ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"名媛荟欢迎您"];
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    NSDictionary *dict = [[UserSession shareUserSession] getUserPublicInfo];
    //tfUserName.text = [dict objectForKey:@"nickName"];
    if ([[dict objectForKey:@"sex"] isEqualToString:@"male"]) {
        //男性
        btnMale.selected = YES;
        btnFemale.selected = NO;
    }
    else {
        //女性
        btnMale.selected = NO;
        btnFemale.selected = YES;
    }
}

- (IBAction)btnAction:(id)sender {
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 101) {
        //提交资料
        [self submitResult];
    }
    else if (btn.tag == 102) {
        //男
        btnMale.selected = YES;
        btnFemale.selected = NO;
    }
    else if (btn.tag == 103) {
        //女
        btnMale.selected = NO;
        btnFemale.selected = YES;
    }
    else if (btn.tag == 104) {
        //完成
        [tfUserName resignFirstResponder];
        [tfInvitationCode resignFirstResponder];
    }
}

- (void)submitResult {
    NSString *userId = [[UserSession shareUserSession] getUserId];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[UserSession shareUserSession] getUserPublicInfo]];
    [dict setObject:tfUserName.text forKey:@"nickName"];
    [dict setObject:btnMale.selected ? @"male" : @"female" forKey:@"sex"];
    [[UserSession shareUserSession] savePublicInfo:dict];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[dict objectForKey:@"nickName"],@"nickName",
                                   [dict objectForKey:@"sex"],@"sex",
                                   tfInvitationCode.text,@"invitated_code",
                                   userId,@"userId",nil];
    [SVProgressHUD showWithStatus:@"正在保存..."];
    [[AppsEngine shareAppsEngine] submitRequest:@"gfModifyUserInfo" param:params onCompletion:^(id jsonResponse) {
        [SVProgressHUD dismiss];
        if ([jsonResponse objectForKey:@"status"] && [[jsonResponse objectForKey:@"status"] isEqualToString:@"success"]) {
            [SVProgressHUD showSuccessWithStatus:@"资料修改保存成功!"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else {
            DXAlertView *alertView = [[DXAlertView alloc] initWithTitle:@"温馨提示" contentText:@"资料修改保存失败，是否重试?" leftButtonTitle:@"取消" rightButtonTitle:@"重试"];
            alertView.rightBlock = ^(void) {
                [self submitResult];
            };
            [alertView show];
        }
    } onError:^(NSError *error) {
        [SVProgressHUD dismiss];
        DXAlertView *alertView = [[DXAlertView alloc] initWithTitle:@"温馨提示" contentText:@"资料修改保存失败，是否重试?" leftButtonTitle:@"取消" rightButtonTitle:@"重试"];
        alertView.rightBlock = ^(void) {
            [self submitResult];
        };
        [alertView show];
    } method:@"GET"];
}

#pragma mark - Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
    if (!iPhone5) {
        [lgScrollView setContentOffset:CGPointMake(0, 40) animated:YES];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [lgScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

#pragma mark - UITextfieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.inputAccessoryView = viewEdittingDone;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    textField.inputAccessoryView = nil;
}


@end
